<?php
require_once('./admin/connexion.php');
require ('./func.req.php');
if ($install) {
    header("Location:./install/index.php");
} else {
    require('./config/tbl.php');
    if ($line_info['maintenance_choix_info'] == "Oui") {
        show_maintenance();
    } else {

        /*
          -----------------------------------
          ---- SCRIPT D'AUTHENTIFICATION ----
          FujiCraft V3.0
          By Fujido
          Contact : thefujido@gmail.com
          -----------------------------------
         */

        session_start(); // On relaye la session
        if (@session_is_registered("authentification")) { // v�rification sur la session authentification (la session est elle enregistr�e ?)
// ici les �ventuelles actions en cas de r�ussite de la connexion
        } else {
            header("Location:login.php?erreur=needlog"); // redirection en cas d'echec
        }

// GESTION DE LA D�connexion
        if (isset($_GET['erreur']) && $_GET['erreur'] == 'logout') { // Test sur les param�tres d'URL qui permettront d'identifier un "contexte" de d�connexion
            $prenom = $_SESSION['prenom']; // On garde le pr�nom en variable pour dire au revoir (soyons polis :-)
            @session_unset("authentification");
            header("Location:login.php?erreur=logout");
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title><?php echo $site; ?> | iConomy - Gestion de compte</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <meta http-equiv="Content-Style-Type" content="text/css" />
                <meta http-equiv="content-language" content="fr,en" />
                <link rel='shortcut icon' type="image/x-icon" href='./images/favicon.ico' />
                <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/tab_news.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/news.css" title="" media="all" />
                <script type="text/javascript" src="./js/news.js"></script>
            </head>
            <body>
                <div id="block">
                    <div id="contentblock">
                        <div class="wrapper">
                            <?php
                            show_header();
                            show_menu();
                            show_newsbox();
                            show_userbox();
                            $user_server = $_SESSION['login'];
                            $query = "SELECT * FROM " . $database . "." . $tb_iconomy . " WHERE username = '$user_server' ";
                            $result = @mysql_query($query) OR DIE("Erreur de la s&eacute;lection de votre compte iConomy : " . mysql_error());
                            $query_ico = "SELECT * FROM " . $database . "." . $tb_iconomy . "";
                            $result_ico = @mysql_query($query_ico) OR DIE("Erreur de la s&eacute;lection des comptes iConomy : " . mysql_error());
                            $line = mysql_fetch_assoc($result);
                            ?>
                            <div class="mainbox">
                                <h2>iConomy - Gestion de compte</h2>
                                <br />
                                <center>
                                    <?php
                                    if (mysql_num_rows($result) == 0) {
                                        ?>
                                        <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" style="margin:10%;" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                            Vous n'&ecirc;tes pas membre du serveur ou vous ne vous &ecirc;tes pas inscrit avec votre pseudo Minecraft.
                                        </div>
                                        <br />
                                        <br />
                                        <a href="./profil.php" title="Retourner &agrave; mon profil" class="btn">Retour &agrave; mon profil</a> <br />
                                        <br />
                                        <?php
                                    } else {
                                        if (isset($_GET['action']) == "transfert") {
                                            $user_add = $_POST['to'];
                                            $balance_add = $_POST['montant'] . ".00";
                                            $query_add = "UPDATE " . $database . "." . $tb_iconomy . " SET balance = balance + '$balance_add' WHERE username = '$user_add'";
                                            $result_add = @mysql_query($query_add) OR DIE("Erreur de la s&eacute;lection du compte iConomy de la personne &agrave; cr&eacute;diter : " . mysql_error());
                                            $query_remove = "UPDATE " . $database . "." . $tb_iconomy . " SET balance = balance - '$balance_add' WHERE username = '$user_server'";
                                            $result_remove = @mysql_query($query_remove) OR DIE("Erreur de la s&eacute;lection de votre compte : " . mysql_error());
                                            ?>
                                            <div id="box" onclick="document.getElementById('box').style.display='none';" style="margin:10%;" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                                Transfert de la somme r&eacute;ussie.
                                            </div>
                                            <br />
                                            <br />
                                            <a href="./iconomy.php" class="btn">Retour &agrave; la gestion de votre compte</a> <br />
                                            <?php
                                        } else {
                                            ?>
                                            <fieldset class="fieldset">
                                                <legend>Compte</legend>
                                                <br />
                                                Bonjour <b><?php echo $line['username']; ?></b>, tu as actuellement <b><?php echo $line['balance']; ?> $</b> sur ton compte. <br />
                                                <br />
                                            </fieldset>
                                            <br />
                                            <br />
                                            <fieldset class="fieldset">
                                                <legend>Transfert d'argent</legend>
                                                <br />
                                                <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?action=transfert">
                                                    <table>
                                                        <tr>
                                                            <td>Vers&eacute; &agrave; :</td>
                                                            <td>
                                                                <select name="to">
                                                                    <?php
                                                                    while ($user = mysql_fetch_assoc($result_ico)) {
                                                                        if ($user['username'] == $_SESSION['login']) {
                                                                            
                                                                        } else {
                                                                            ?>
                                                                            <option value="<?php echo $user['username']; ?>"><?php echo $user['username']; ?></option>
                                                                            <?php
                                                                        }
                                                                    }
                                                                    ?>
                                                                </select>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>Montant :</td>
                                                            <td>
                                                                <select name="montant">
                                                                    <?php
                                                                    for ($i = 10; $i <= $line['balance']; $i+=10) {
                                                                        echo "<option>" . $i . "</option>";
                                                                    }
                                                                    ?>
                                                                </select>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                    <br />
                                                    <input type="submit" value="Transf&eacute;rer l'argent" name="transfert" class="btn" /><br />
                                                    <br />
                                                </form>
                                            </fieldset>
                                            <br />
                                            <br />
                                            <a href="./profil.php" title="Retourner &agrave; mon profil" class="btn">Retour &agrave; mon profil</a> <br />
                                            <br />
                                            <?php
                                        }
                                    }
                                    ?>
                                </center>
                                <br />
                            </div>
                            <?php show_footer(); ?>
                        </div>
                    </div>
                </div>
            </body>
        </html>
        <?php
    }
}
?>